package systems.database.query.condition;

import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Expression {
  private Literal literal1;
  private HashMap<Literal, ComparatorEnum> literalMap;

  public Expression(Literal literal1)
  {
    this.literal1 = literal1;
    this.literalMap = new HashMap<Literal, ComparatorEnum>();
  }

  public void addComparatorAndLiteral(ComparatorEnum comparator, Literal literal)
  {
    this.literalMap.put(literal, comparator);
  }

  public String getExpressionString()
  {
    Pattern isNumberPattern = Pattern.compile("[+-]?[0-9]*([.]?[0-9]*)?");
    Matcher isNumberMatcher;
    Set<Entry<Literal, ComparatorEnum>> set = this.literalMap.entrySet();
    String expression = "";
    expression += "`"+literal1.getValue()+"`";
    for(Entry<Literal, ComparatorEnum> oneEntry : set)
    {
      String value = oneEntry.getKey().getValue();
      isNumberMatcher = isNumberPattern.matcher(value);
      if(!isNumberMatcher.matches())
        value = "\""+value+"\"";
      expression += " "+ComparatorEnum.getValue(oneEntry.getValue())+" "+value;
    }

    return expression;
  }
}